#Author:Jason Song
import copy
import math
dic = {
    "huixin":35737.5,
    "huixiao":12506.25,
    "wushuang":34458.75,
    "fangyu":19091.25,
    "pofang":35737.5,# 奉天证道
    # "pofang":15344.2,# 世外蓬莱
    "jiasu":43856.25,
    "xinfa_gongji":1.7998, #心法根骨提供的额外内功攻击
    }
def_dic = {
        "110":[3369,19091.25],
        "111":[5034,20134.905], # 奉天证道
        # "111":[1924,7698.34], #世外蓬莱
        "112":[7060,21178.560],
        "113":[11966,22222.215],
        "114":[12528,23265.870],
        }
acc_dic_1 = {
    "1":[0,0],
    "0.9375":[43,0.09765625],
    "0.875":[2956,6.73828125],
    "0.8125":[6298,14.35546875],
    "0.75":[10153,23.14453125],
}
acc_dic_15 = {
    "1.5":[0,0],
    "1.4375":[43,0.09765625],
    "1.375":[1928,4.39453125],
    "1.3125":[4027,9.1796875],
    "1.25":[6298,14.35546875],
    "1.1875":[8782,20.01953125],
}
class Judge:
    def __init__(self,res,skill,buff):
        # print(buff)
        self.fin_damage = [0, 0]
        self.skill_name = skill[8]
        # print(self.skill_name,buff)
        self.weapon = 0 # 武伤，药宗没有所以直接0
        self.skill_basic_damage = skill[0] # 技能基础伤害
        self.skill_damage_per = skill[1] # 技能伤害系数
        self.skill_weapon_damage = skill[2] # 技能武器伤害系数
        self.jichugongji = int(res["jichugongji"]) # 人物初始基础攻击
        self.huixin = float(res["huixin"])*0.01 + skill[4] # 人物基础会心+技能奇穴会心
        self.huixiao = float(res["huixiao"])*0.01 + skill[5] # 人物基础会效+技能奇穴会效
        self.jiasu = int(res["jiasu"])
        self.wushuang = float(res["wushuang"])
        self.gengu = int(res["gengu"])
        self.pofang = float(res["pofang"])
        self.pozhao = float(res["pozhao"])
        self.limit_reduce_defence = 0 #B类固定减防御
        self.reduce_defence = 0
        self.target = res["target"] # 目标等级 1-110级，2-111级，3-112级，4-113级，5-114级
        self.skill = skill
        self.yujin = 0
        if res["qixue_7"] == "2" or "谢枝" in res["buff"]:
            # self.yujin = -0.04
            pass

        # 战斗BUFF+阵眼+小吃小药 带来的额外收益
        self.ex_gongji = 0
        self.ex_pofang = 0
        self.ex_huixin = 0
        self.ex_huixiao = 0
        self.zengshang = 1

        #计算基础破防
        self.pofang -= self.gengu * 0.47/ dic["pofang"]
        self.ex_pofang += self.gengu * 0.47/ dic["pofang"]




        # 秘籍奇穴增伤
        self.miji_zengshang = skill[3]
        self.zengshang = 1 + self.miji_zengshang
        if res["zhenyan"] == "7":
            self.zengshang += 0.03


        # 伤衣伤帽大附魔
        if "伤帽-破防+551" in res["buff"]:
            self.pofang += round(55100/dic["pofang"],2)
        if "伤衣-攻击+395" in res["buff"]:
            self.jichugongji += 395

        # 袖气
        if "袖气" in res["buff"]:
            self.gengu += 111
            self.jichugongji += 111*0.18
            self.pofang += 111*0.3/dic["pofang"]
            self.ex_pofang += 111 * 0.47 / dic["pofang"]
            self.huixin += (111*0.64)/dic["huixin"]

        # 处理小吃小药
        if "fuzhuyaopin" in res:
            if res["fuzhuyaopin"] == "1":
                pass
            elif res["fuzhuyaopin"] == "2":
                k = 1
                m = int(200*k)
                self.gengu += m
                self.ex_pofang += 100*m *0.47/dic["pofang"]
                self.huixin += m *0.64/dic["huixin"]
            elif res["fuzhuyaopin"] == "3":
                k = 1
                m = int(146*k)
                self.gengu += m
                self.ex_pofang += 100*m *0.47/dic["pofang"]
                self.huixin += m *0.64/dic["huixin"]
            else:
                pass
        if "zengqiangyaopin" in res:
            if res["zengqiangyaopin"] == "1":
                pass
            elif res["zengqiangyaopin"] == "2":
                self.jichugongji += 481
            elif res["zengqiangyaopin"] == "3":
                self.pofang += 89400/dic["pofang"]

            elif res["zengqiangyaopin"] == "4":
                self.huixin += 894/dic["huixin"]

            elif res["zengqiangyaopin"] == "5":
                self.pozhao += 894
            elif res["zengqiangyaopin"] == "6":
                self.jichugongji += 349
            else:
                pass
        if "fuzhushipin" in res:
            if res["fuzhushipin"] == "1":
                pass
            elif res["fuzhushipin"] == "2":
                k = 1
                m = int(156*k)
                self.gengu += m
                self.ex_pofang += 100*m *0.47/dic["pofang"]
                self.huixin += m *0.64/dic["huixin"]
            elif res["fuzhushipin"] == "3":
                k = 1
                m = int(113*k)
                self.gengu += m
                self.jichugongji += m*0.18
                self.ex_pofang += 100*m *0.47/dic["pofang"]
                self.huixin += m *0.64/dic["huixin"]
            else:
                pass
        if "zengqiangshipin" in res:
            if res["zengqiangshipin"] == "1":
                pass
            elif res["zengqiangshipin"] == "2":
                self.jichugongji += 374
            elif res["zengqiangshipin"] == "3":
                self.pofang += 69500 / dic["pofang"]
            elif res["zengqiangshipin"] == "4":
                self.huixin += 695 / dic["huixin"]
            elif res["zengqiangshipin"] == "5":
                self.pozhao += 695
            elif res["zengqiangshipin"] == "6":
                self.jichugongji += 272
            else:
                pass
        if "wuqimoshi" in res:
            if res["wuqimoshi"] == "1":
                pass
            elif res["wuqimoshi"] == "2":
                self.jichugongji += 320
            elif res["wuqimoshi"] == "3":
                self.jichugongji += 233
            else:
                pass
        # 家园酒
        if "jiayuanjiu" in res:
            if res["jiayuanjiu"] in ["5","6","7"]:
                jiu_k = 0
                if res["jiayuanjiu"] == "5":
                    jiu_k += 1
                elif res["jiayuanjiu"] == "6":
                    jiu_k += 3
                elif res["jiayuanjiu"] == "7":
                    jiu_k += 4
                else:
                    pass
                m = int(24*jiu_k) # 24是酒加的根骨
                self.gengu += m
                self.ex_pofang += 100*m *0.47/dic["pofang"]
                self.huixin += m *0.64/dic["huixin"]
        # 家园烹饪
        if "jiayuancai" in res:
            if res["jiayuancai"] == "2":
                self.jichugongji += 149
            elif res["jiayuancai"] == "3":
                self.huixin += 277/dic["huixin"]
            elif res["jiayuancai"] == "4":
                self.pofang += 27700/dic["pofang"]
            elif res["jiayuancai"] == "5":
                self.pozhao += 277
            elif res["jiayuancai"] == "6":
                self.wushuang += 27700 / dic["wushuang"]
            elif res["jiayuancai"] == "8":
                self.jichugongji += 402
            elif res["jiayuancai"] == "9":
                self.huixin += 750 / dic["huixin"]
            elif res["jiayuancai"] == "10":
                self.pofang += 75000 / dic["pofang"]
            elif res["jiayuancai"] == "11":
                self.pozhao += 750
            elif res["jiayuancai"] == "12":
                self.wushuang += 75000 / dic["wushuang"]

        # 节日食物
        if "jierishiwu" in res:
            if res["jierishiwu"] == "2":
                self.jichugongji += 479
            elif res["jierishiwu"] == "3":
                self.jichugongji += 157
            elif res["jierishiwu"] == "4":
                self.pofang += 89100/dic["pofang"]
            elif res["jierishiwu"] == "5":
                self.pofang += 29200/dic["pofang"]
            elif res["jierishiwu"] == "7":
                self.jichugongji += 192
            elif res["jierishiwu"] == "8":
                self.jichugongji += 577
            elif res["jierishiwu"] == "9":
                self.jichugongji += 493
            elif res["jierishiwu"] == "10":
                self.wushuang += 6
            elif res["jierishiwu"] == "11":
                self.huixin += 0.06
            elif res["jierishiwu"] == "12":
                self.jichugongji += 327
                self.huixin += 609/dic["huixin"]
            elif res["jierishiwu"] == "13":
                self.jichugongji += 107
                self.huixin += 200/dic["huixin"]
            elif res["jierishiwu"] == "14":
                self.jichugongji += 327
                self.pofang += 60900/dic["pofang"]
            elif res["jierishiwu"] == "15":
                self.jichugongji += 107
                self.pofang += 20000 / dic["pofang"]
            elif res["jierishiwu"] == "16":
                self.jichugongji += 327
                self.wushuang += 60900/dic["wushuang"]
            elif res["jierishiwu"] == "17":
                self.jichugongji += 107
                self.wushuang += 20000 / dic["wushuang"]



        if "奉天·二十四桥" in res["med"]:
            self.jichugongji += 214
            self.huixin += 397/dic["huixin"]
            self.pozhao += 397
        if "蒸鱼" in res["med"]:
            self.wushuang += 51700/dic["wushuang"]
        if "水煮鱼" in res["med"]:
            self.wushuang += 10000 / dic["wushuang"]
            self.pozhao += 100
        if "同泽宴" in res["med"]:
            self.wushuang += 13000 / dic["wushuang"]
            self.pozhao += 130
        if "梅花盾" in res["buff"]:
            self.reduce_defence += 0.15*0.01*float(res["zengyifugai"]["meihuadun"]) #无视百分比防御

        if "套装+双会" in res["buff"]: #测试触发几率为9%，覆盖率为90.3%
            self.ex_huixin += round(0.04*0.903,2)
            self.ex_huixiao += round(0.041*0.903,2)

            # self.ex_gongji += int(0.0996*self.jichugongji*0.5168)
        # print(res["buff"])
        if "振奋" in res["buff"]:
            self.pofang += float(2500*int(res["zengyifugai"]["zhenfen"]))/dic["pofang"]#未实测

        # if "腰坠·风特效" in res["buff"]:#2883点破防 持续15sCD3分钟 4165
        #     self.ex_pofang += (288300*15/180)/dic["pofang"]
        if "风" in buff and "腰坠·风特效" in res["buff"]: # 判断buff是否存在
            self.ex_pofang += 416500/dic["pofang"]
        if "武器·云间近" in res["buff"]:#490点攻击 1层49 10层490
            self.jichugongji += 490
        if "武器·远行之念" in res["buff"]:#530点攻击 1层53 10层530
            self.jichugongji += 530
        if "清涓" in res["buff"]:
            self.ex_pofang += float(0.09765*self.pofang) #基础破防
        if "破苍穹" in res["buff"]:
            # self.ex_huixin += 0.05 #20210621取消小队会心加成
            self.ex_huixiao += 0.10
        if "弘法" in res["buff"]:
            self.ex_gongji += int(0.29297*0.01*float(res["zengyifugai"]["hongfa"])*self.jichugongji)
        if "分澜" in res["buff"]:
            self.ex_gongji += int(0.01*float(res["zengyifugai"]["fenlan"])*0.14928*self.jichugongji)
        if "朝圣言" in res["buff"]:
            self.ex_gongji += int(0.01*float(res["zengyifugai"]["chaoshengyan"])*0.1953*self.jichugongji)
        if "朝圣言-PLUS" in res["buff"]:
            self.ex_gongji += int(0.01*float(res["zengyifugai"]["chaoshengyan"])*0.29297*self.jichugongji)
        if "凄骨" in buff:
                self.reduce_defence += 0.5 #无视百分比防御
        if "相使" in buff: # 判断buff是否存在
                self.ex_gongji += int(0.14928*self.jichugongji)
        if "千枝绽蕊" in buff and res["qixue_12"] == '2': # 判断buff是否存在
                self.ex_pofang += float(0.3*self.pofang)
        if "立地成佛" in res["buff"]:
            self.limit_reduce_defence += 0.15
        if "寒啸千军" in res["buff"]:
            self.ex_pofang += float(0.2*0.01*float(res["zengyifugai"]["hanxiaoqianjun"])*self.pofang)#未实测
        if "疏狂" in res["buff"]:
            self.ex_gongji += int(0.29297*0.01*float(res["zengyifugai"]["shukuang"])*self.jichugongji)
        if "号令三军" in res["buff"]:
            self.ex_gongji += int(0.06*self.jichugongji)

        if "飞剑·意" in buff:
            self.ex_huixin += 0.047009
            self.ex_huixiao += 0.063968
        if "春节阵·朱雀" in buff:
            self.ex_huixin += 1
        if "春节阵·封门" in res["buff"]:
            self.jichugongji += 1800




        #阵眼效果
        if res["zhenyan"] == "2": #花间阵
            # 基础攻击提高5 % int(4.976 %)
            # 基础破防提高 int(0.29297)
            self.ex_gongji += int(0.04876*self.jichugongji)
            self.ex_pofang += float(0.29297*self.pofang)
        if res["zhenyan"] == "3": #气纯阵
            self.wushuang += 0.0195
            self.ex_huixin += 0.08
            self.ex_huixiao += 0.15
        if res["zhenyan"] == "4": #大师阵
            self.wushuang += 0.0195
            self.ex_gongji += int(0.14928*self.jichugongji)
            self.ex_pofang += float(0.09765*self.pofang)
        if res["zhenyan"] == "5": # 田螺阵
            self.ex_gongji += int(0.04876 * self.jichugongji)
            self.reduce_defence += 0.05  # 无视百分比防御
            self.ex_huixin += 0.05
            self.ex_huixiao += 0.15
        if res["zhenyan"] == "6": #毒经阵
            self.ex_gongji += int(0.04876 * self.jichugongji)
            self.ex_huixin += 0.03
            self.ex_huixiao += 0.10
            self.ex_pofang += float(0.5*0.09765*self.pofang)
        if res["zhenyan"] == "9": #莫问阵
            self.wushuang += 0.0195
            self.ex_gongji += int(0.09765 * self.jichugongji)
            self.ex_huixin += 0.08
        if res["zhenyan"] == "7": #无方阵
            self.ex_huixin += 0.05
            self.ex_gongji += int(0.04876 * self.jichugongji)



        if "戒火斩" in res["buff"]:
            self.zengshang = self.zengshang * 1.0282
        if "伤腰大附魔" in res["buff"]:
            self.zengshang = self.zengshang * 1.013

        # 非侠士增伤
        # self.zengshang = self.zengshang * 1.04977 没有非侠士增伤了


        self.fin_gongji = self.jichugongji+int(dic["xinfa_gongji"]*self.gengu)+int(self.ex_gongji)
        self.fin_pofang = self.pofang+self.ex_pofang
        self.fin_gengu = self.gengu
        self.fin_huixin = self.huixin+self.ex_huixin
        if self.fin_huixin > 1:
            self.fin_huixin = 1
        self.fin_huixiao = self.huixiao+self.ex_huixiao
        self.fin_jiasu = self.jiasu
        self.fin_wushuang = self.wushuang
        if self.skill_name == "飘黄":
            self.reduce_defence = 1

        if self.reduce_defence >1:
            self.reduce_defence = 1


    #防御减伤计算,limit_reduce_defence是B类固定减基础防御，reduce_defence是无视百分比防御
    def guo_defence(self,limit_reduce_defence,reduce_defence):
        tar_defence_level_110 = (def_dic["110"][0] * (1 - limit_reduce_defence)) * (1 - reduce_defence)
        tar_defence_level_111 = (def_dic["111"][0]*(1-limit_reduce_defence))*(1-reduce_defence)
        tar_defence_level_112 = (def_dic["112"][0]*(1-limit_reduce_defence))*(1-reduce_defence)
        tar_defence_level_113 = (def_dic["113"][0]*(1-limit_reduce_defence))*(1-reduce_defence)
        tar_defence_level_114 = (def_dic["114"][0]*(1-limit_reduce_defence))*(1-reduce_defence)
        def_110 = int(tar_defence_level_110 * 1024 / (tar_defence_level_110 + def_dic["110"][1]))
        def_111 = int(tar_defence_level_111*1024/(tar_defence_level_111+def_dic["111"][1]))
        def_112 = int(tar_defence_level_112*1024/(tar_defence_level_112+def_dic["112"][1]))
        def_113 = int(tar_defence_level_113*1024/(tar_defence_level_113+def_dic["113"][1]))
        def_114 = int(tar_defence_level_114*1024/(tar_defence_level_114+def_dic["114"][1]))
        res = [def_110,def_111,def_112,def_113,def_114]
        return res

    #计算郭氏破防值
    def guo_pofang(self,fin_pofang):
        pofang_value = int(fin_pofang*0.01*dic["pofang"])
        guo_pf = int(pofang_value*1024/dic["pofang"])
        return guo_pf
    # 原始伤害
    def damage_ori(self):
        damage_ori = int(self.skill_basic_damage) + int(float(self.fin_gongji)*self.skill_damage_per)+int(self.weapon*self.skill_weapon_damage)
        return damage_ori
    # 计算伤害
    def d(self):
        guo_pf = self.guo_pofang(self.fin_pofang)
        guo_fy = self.guo_defence(self.limit_reduce_defence, self.reduce_defence)[int(self.target) - 1]
        Y = 1024+guo_pf-int((1024+guo_pf)*guo_fy/1024)
        damage_basic = int(self.damage_ori()*Y/1024) # 基准伤害(破防伤害)
        # guo_fy = self.guo_defence(self.limit_reduce_defence, self.reduce_defence)[int(self.target) - 1]
        # damage_basic = int(self.damage_ori() * (1 + 0.01 * self.fin_pofang) * (1 - (guo_fy / 1024)))
        # print("最终无双",1+self.fin_wushuang*0.01)
        damage_basic_wushuang = damage_basic*(1+self.fin_wushuang*0.01) #无双加成后的伤害
        # 计算会心伤害
        # 郭氏会效值
        guo_huixiao = int((self.fin_huixiao-1.75)*1024)
        damage_basic_wushuang_huixin = int(damage_basic_wushuang*1.75)+int(damage_basic_wushuang * guo_huixiao/1024)- int(damage_basic_wushuang * ((guo_huixiao/1024+0.75)*(self.yujin*35737.5/9588.75)))
        damage_final = ((1-self.fin_huixin)*damage_basic_wushuang+self.fin_huixin*damage_basic_wushuang_huixin)*self.zengshang
        return damage_final,self.skill_name,self.skill
    # 单独展示命中/会心伤害
    def t(self):
        guo_pf = self.guo_pofang(self.fin_pofang)
        guo_fy = self.guo_defence(self.limit_reduce_defence,self.reduce_defence)[int(self.target)-1]
        Y = 1024+guo_pf-int((1024+guo_pf)*guo_fy/1024)
        damage_basic = int(self.damage_ori()*Y/1024) # 基准伤害(破防伤害)
        # print("最终无双",1+self.fin_wushuang*0.01)
        damage_basic_wushuang = damage_basic*(1+self.fin_wushuang*0.01)*self.zengshang #无双加成后的伤害
        # 计算会心伤害
        # 郭氏会效值
        guo_huixiao = int((self.huixiao-1.75)*1024)
        # print("郭氏会效值",guo_huixiao)
        damage_basic_wushuang_huixin = (int(damage_basic_wushuang*1.75)+int(damage_basic_wushuang * guo_huixiao/1024)- int(damage_basic_wushuang * ((guo_huixiao/1024+0.75)*(self.yujin*35737.5/9588.75))))*self.zengshang
        return damage_basic_wushuang,damage_basic_wushuang_huixin
    def zhen(self):
        wufangzhen_dic = {
            '0': '200',
            '1': '240',
            '2': '288',
            '3': '346',
            '4': '415',
            '5': '498',
            '6': '597',
            '7': '717',
            '8': '860',
            '9': '1032',
            '10': '1238',
            '11': '1486',
            '12': '1783',
            '13': '2140',
            '14': '2568',
            '15': '3081',
            '16': '3698',
            '17': '4437',
            '18': '5325',
            '19': '6390',
            '20': '7029',
            '21': '7731',
            '22': '8505',
            '23': '9355',
            '24': '10291',
            '25': '11320',
            '26': '12452',
            '27': '13697',
            '28': '15066',
            '29': '16573',
            '30': '18230',
            '31': '20053',
            '32': '22059',
            '33': '24264',
            '34': '26691',
            '35': '29360',
            '36': '32296',
            '37': '35526',
            '38': '39078',
            '39': '42986',
            '40': '47285',
            '41': '52013',
            '42': '57214',
            '43': '62936',
            '44': '69229',
            '45': '76152',
            '46': '83768',
            '47': '92144',
            '48': '101359',
            '49': '111495',
            '50': '122644',
            '51': '134909',
            '52': '148399',
            '53': '163239',
            '54': '179563',
            '55': '197520',
            '56': '217272',
            '57': '238999',
            '58': '262899',
            '59': '289189',
            '60': '303648',
            '61': '318830',
            '62': '334772',
            '63': '351510',
            '64': '369086',
            '65': '387540',
            '66': '406917',
            '67': '427263',
            '68': '448626',
            '69': '471058',
            '70': '494611',
            '71': '519341',
            '72': '545308',
            '73': '572574',
            '74': '601202',
            '75': '631262',
            '76': '662825',
            '77': '695967',
            '78': '730765',
            '79': '767303',
            '80': '782649',
            '81': '798302',
            '82': '814268',
            '83': '830554',
            '84': '847165',
            '85': '864108',
            '86': '881390',
            '87': '899018',
            '88': '916998',
            '89': '935338',
            '90': '954045',
            '91': '973126',
            '92': '992589',
            '93': '1012440',
            '94': '1032689',
            '95': '1053343',
            '96': '1074410',
            '97': '1095898',
            '98': '1117816',
            '99': '1140172',
        }
        zhen_gongji = float(self.fin_gongji)
        zhen_huixin = float(self.fin_huixin) * dic["huixin"]
        zhen_huixiao = (float(self.fin_huixiao)-1.75) * dic["huixiao"]
        zhen_pofang = float(self.fin_pofang) * dic["pofang"]/100
        zhen_huixin_per = math.ceil(10000 * zhen_huixin / dic["huixin"]) / 100
        zhen_huixiao_per = math.ceil(10000 * zhen_huixiao / dic["huixiao"]) / 100
        # print(self.jichugongji,zhen_gongji,zhen_pofang,zhen_huixin_per,zhen_huixiao_per)
        nskillCof = 0.8
        nHuiXinPercent = zhen_huixin / 35737.5 + zhen_huixin_per / 10000
        nHuiXiaoPercent = zhen_huixiao / 12506.25 + zhen_huixiao_per / 10000 + 1.75
        nPoFangPercent = zhen_pofang / 35737.5
        ndamage = zhen_gongji * nskillCof * (nHuiXinPercent * nHuiXiaoPercent + (1 - nHuiXinPercent)) * (1 + nPoFangPercent)
        # damage=gongji*0.8*(huixin/35737.5+huixin_per/10000) * (huixiao/12506.25+(huixiao_per/10000+1.75) +(1+huixin/35737.5+huixin_per/10000))*(1+pofang/35737.5)
        # print(ndamage)
        for i in wufangzhen_dic.keys():
            if float(wufangzhen_dic[i]) > ndamage:
                # print(int(i) + 1, self.jichugongji,zhen_gongji,zhen_pofang,zhen_huixin_per,zhen_huixiao_per,self.skill_name,self.fin_huixiao)
                self.skill_basic_damage = copy.copy(wufangzhen_dic[i])
                self.fin_damage = [self.t()[0],self.t()[1],i]
                break
        # print(self.fin_damage)
        return self.fin_damage
    def print_obj(obj): 
        print(obj.__dict__)